import cv2
image=cv2.imread('hat.png',cv2.IMREAD_GRAYSCALE)
cv2.imshow("Input",image)
#3*3的结构
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
#进行运算
gradient=cv2.morphologyEx(image,cv2.MORPH_GRADIENT,kernel)
gradient1=cv2.morphologyEx(image,cv2.MORPH_ERODE,kernel)
gradient2=cv2.morphologyEx(image,cv2.MORPH_DILATE,kernel)
gradient3=cv2.morphologyEx(image,cv2.MORPH_OPEN,kernel)
gradient4=cv2.morphologyEx(image,cv2.MORPH_CLOSE,kernel)
gradient5=cv2.morphologyEx(image,cv2.MORPH_TOPHAT,kernel)
gradient6=cv2.morphologyEx(image,cv2.MORPH_BLACKHAT,kernel)
cv2.imshow("Input",image)
cv2.imshow("GRADIENT",gradient)
cv2.imshow("ERODE",gradient1)
cv2.imshow("DILATE",gradient2)
cv2.imshow("OPEN",gradient3)
cv2.imshow("CLOSE",gradient4)
cv2.imshow("TOPHAT",gradient5)
cv2.imshow("BLACKHAT",gradient6)
cv2.waitKey()
cv2.destroyAllWindows()